﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Googler.Utilities.Http;
using System.IO;
using System.Xml.XPath;
using System.Web.Script.Serialization;
using Googler.BLL;

namespace HodhodNews.Aggregation
{
    public class YahooNewsAggregationProvider : NewsAggregationProvider
    {

        public override List<News> Aggregate(AggregationOptions options)
        {
            IEnumerable<News> list = new List<News>();
            foreach (var key in options.Keywords)
            {
                var downloader = new HttpDownloader(ResolveUrl(key.Name), "", "");
                string result = downloader.GetPage();
                List<News> news = ConvertToNews(result);
                news.ForEach(n =>
                {
                    if (n.Keywords.Find(k => k.ID == k.ID) == null)
                    {
                        n.Keywords.Add(key);
                    }
                });
                list = list.Union(news);
            }
            return list.ToList();

        }
        class YahooNewsResult
        {

            public string Result;
            public string Title;
            public string Summary;
            public string Url;
            public string ClickUrl;
            public string NewsSource;
            public string NewsSourceUrl;
            public string Language;
            public string PublishDate;
            public string ModificationDate;
            public string Thumbnail;
        }
        List<News> ConvertToNews(string result)
        {
            List<News> list = new List<News>();

            //var serializer = new JavaScriptSerializer();
            //var x = serializer.Deserialize(json, typeof(YahooNewsResult));
            //return null;
            var reader = new StringReader(result);
            var document = new XPathDocument(reader);
            var navigator = document.CreateNavigator();
            navigator.MoveToChild(XPathNodeType.All);
            navigator.MoveToChild(XPathNodeType.All);


            do
            {
                var unixTimeBase = new DateTime(1970, 1, 1, 0, 0, 0);
                var children = navigator.SelectChildren(XPathNodeType.All);
                var a = new News();
                a.Aggregator = "Yahoo News";
                list.Add(a);
                while (children.MoveNext())
                {
                    if (children.Current.LocalName == "Title")
                        a.Title = children.Current.Value;
                    else if (children.Current.LocalName == "Summary")
                        a.SummaryHtml = children.Current.Value;
                    else if (children.Current.LocalName == "Url")
                        a.Link = children.Current.Value;
                    else if (children.Current.LocalName == "NewsSource")
                        a.Publisher = children.Current.Value;
                    else if (children.Current.LocalName == "PublishDate")
                        a.PublishDate = unixTimeBase.AddSeconds(Convert.ToInt32(children.Current.Value));
                    else if (children.Current.LocalName == "ModificationDate")
                        a.ModificationDate = unixTimeBase.AddSeconds(Convert.ToInt32(children.Current.Value));
                }
            } while (navigator.MoveToNext());

            return list;
        }
        string ResolveUrl(string keyword)
        {
            return this.ServiceUrl + "query=" + keyword + "&results=50&type=any&language=ar&output=xml&";
        }

        public string ServiceUrl
        {
            get
            {
                return "http://search.yahooapis.com/NewsSearchService/V1/newsSearch?appid=rej13PjV34EnzusA_moownkb4_f4tfZPYjweylt_iarDYdEsyARO6FCzPHsJv4EkXAE-&";
            }
        }
    }
}
